package com.woniu.controller;


import com.woniu.domain.Classify;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.ClassifyService;
import org.springframework.web.bind.annotation.GetMapping;
import com.woniu.domain.Classify;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.service.ClassifyService;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.domain.Classify;
import com.woniu.dto.Result;
import com.woniu.dto.StatusCode;
import com.woniu.mapper.ClassifyMapper;
import com.woniu.service.ClassifyService;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/classify")
public class ClassifyController {

    @Resource
    private ClassifyService classifyService;

    @GetMapping("onetwo")
    public Result getOneTwoClassify(){
        List<Classify> oneTwoClassify = classifyService.getOneTwoClassify();
        return new Result(true, StatusCode.OK,"查询一二级分类成功",oneTwoClassify);
    }

@GetMapping("findclassifyname")
    public Result<Classify> findclassifyname(){
    List<Classify> list=classifyService.list(null);
    return new Result<>(true, StatusCode.OK,"查询成功",list);
}

    @Resource
    private RedisTemplate<String,Classify> redisTemplate;

    //查询所有的一级分类
    @GetMapping("getFirstAll")
    public Result getFirstAll(){
        //从Redis中查询数据
        ListOperations<String, Classify> operations = redisTemplate.opsForList();
        List<Classify> classifies = operations.range("firstClassifies", 0, -1);
        if (ObjectUtils.isEmpty(classifies)) {
            synchronized (this){
                //防止高并发访问时多次访问数据库
                classifies=operations.range("firstClassifies", 0, -1);
                if (ObjectUtils.isEmpty(classifies)) {
                    //Redis中没有数据，从数据库中查询
                    QueryWrapper<Classify> wrapper = new QueryWrapper<>();
                    wrapper.eq("level",1);
                    classifies = classifyService.list(wrapper);
                    //将其存入Redis
                    operations.leftPushAll("firstClassifies",classifies);
                    System.out.println("数据库查询");
                }else{
                    System.out.println("Redis查询");
                }
            }
        }
        return new Result(true, StatusCode.OK,"查询一级分类成功",classifies);
    }

    //根据商品大类查询商品小类
    @GetMapping("smell")
    public Result smell(Integer cid){
        if (ObjectUtils.isEmpty(cid)){
            return null;
        }else {
            List<Classify> list = classifyService.smell(cid);
            return new Result(true, StatusCode.OK,"查询职业小类",list);
        }
    }
}

